package org.hbgl.geoentity.db.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.hbgl.geoentity.db.dao.GeDepartmentMapper;
import org.hbgl.geoentity.db.domain.GeDepartment;
import org.hbgl.geoentity.db.domain.GeDepartmentExample;
import org.hbgl.geoentity.db.util.EnumFile;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 代码功能：
 * 开发日期：2022/4/26
 * 作者：蒋海仑
 * 联系方式：
 * 版本更迭说明：
 * 版权信息：河北广联信息技术有限公司
 */
@Service
public class GeDepartmentService {
    @Resource
    private GeDepartmentMapper departmentMapper;

    public List<GeDepartment> querySelective(Integer unitId, String keywords, Integer page, Integer limit, String sort, String order) {
        GeDepartmentExample example = new GeDepartmentExample();
        GeDepartmentExample.Criteria criteria = example.or();

        criteria.andUnitIdEqualTo(unitId);
        if (StringUtils.isNotBlank(keywords)) {
            criteria.andDepartmentNameLike("%" + keywords + "%");
        }
        criteria.andStateNotEqualTo(EnumFile.CommonState.Delete);

        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return departmentMapper.selectByExample(example);
    }

    public List<GeDepartment> querySelective(Integer unitId) {
        GeDepartmentExample example = new GeDepartmentExample();
        GeDepartmentExample.Criteria criteria = example.or();

        criteria.andUnitIdEqualTo(unitId).andStateNotEqualTo(EnumFile.CommonState.Delete);

        return departmentMapper.selectByExample(example);
    }

    public GeDepartment findById(Integer unitId, Integer id) {
        GeDepartmentExample example = new GeDepartmentExample();
        example.or().andUnitIdEqualTo(unitId).andIdEqualTo(id).andStateNotEqualTo(EnumFile.CommonState.Delete);
        return departmentMapper.selectOneByExample(example);
    }

    public List<GeDepartment> findByDepartmentName(Integer unitId, Integer parentId, String departmentName) {
        GeDepartmentExample example = new GeDepartmentExample();
        example.or().andUnitIdEqualTo(unitId).andParentIdEqualTo(parentId).andDepartmentNameEqualTo(departmentName).andStateNotEqualTo(EnumFile.CommonState.Delete);
        return departmentMapper.selectByExample(example);
    }

    public List<GeDepartment> findByDepartmentName(Integer unitId, Integer parentId, String departmentName, Integer id) {
        GeDepartmentExample example = new GeDepartmentExample();
        example.or().andUnitIdEqualTo(unitId).andParentIdEqualTo(parentId).andDepartmentNameEqualTo(departmentName).andIdNotEqualTo(id).andStateNotEqualTo(EnumFile.CommonState.Delete);
        return departmentMapper.selectByExample(example);
    }

    public void add(GeDepartment department) {
        department.setState(EnumFile.CommonState.Normal);
        department.setAddTime(LocalDateTime.now());
        department.setEditTime(LocalDateTime.now());
        departmentMapper.insertSelective(department);
    }

    public int updateById(GeDepartment department) {
        department.setEditTime(LocalDateTime.now());
        return departmentMapper.updateByPrimaryKeySelective(department);
    }

    public List<GeDepartment> findByParentId(Integer unitId, Integer parentId) {
        GeDepartmentExample example = new GeDepartmentExample();
        example.or().andUnitIdEqualTo(unitId).andParentIdEqualTo(parentId).andStateNotEqualTo(EnumFile.CommonState.Delete);
        return departmentMapper.selectByExample(example);
    }

    public int deleteByIdLogical(Integer id) {
        GeDepartment department = new GeDepartment();
        department.setId(id);
        department.setState(EnumFile.CommonState.Delete);
        department.setEditTime(LocalDateTime.now());
        return departmentMapper.updateByPrimaryKeySelective(department);
    }
}
